package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.woniuxy.entity.*;
import com.woniuxy.service.ProductionWareHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/productionwarehouse")
@Api(tags = "生产仓库接口")
public class ProductionWareHouseController {
    @Autowired
    ProductionWareHouseService productionWareHouseService;
    @GetMapping("/findAll/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取成品库存表分页数据")
    public ResponseEntity findAll(@ApiParam(name="currentPage",value = "当前页码",required = true ) @PathVariable int currentPage,
                                  @ApiParam(name = "pageSize",value = "页面大小",required = true)@PathVariable int pageSize){
        return productionWareHouseService.findAll(currentPage,pageSize);
    }
    @GetMapping("/finddeliveryrecordEdit")
    @ApiOperation(value = "获取发货记录表的全部信息")
    public ResponseEntity finddeliveryrecordEdit(){
        return productionWareHouseService.finddeliveryrecordEdit();
    }
    @GetMapping("/warehousingrecord")
    @ApiOperation(value = "获取入库详情表")
    public ResponseEntity findWarehousingRecord(){
        return productionWareHouseService.findWarehousingRecord();
    }
    @GetMapping("/inventorydetails")
   @ApiOperation(value = "获取库存详情表")
    public ResponseEntity findinventorydetails(){
        return productionWareHouseService.findinventorydetails();
    }
    @GetMapping("/search/{material_code}")
    @ApiOperation(value = "通过编码查询")
    public ResponseEntity search(@ApiParam(name = "material_code",value = "产品编码")@PathVariable String  material_code){
        return productionWareHouseService.search(material_code);
    }
    @PostMapping("/add")
    @ApiOperation(value = "新增发货")
    public ResponseEntity addOne(@ApiParam(name ="deliveryrecord",value = "新增发货") @RequestBody Deliveryrecord deliveryrecord){
        System.out.println(deliveryrecord);
        return productionWareHouseService.addOne(deliveryrecord);
    }
    @GetMapping("/findAlldelivery_record/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取成品发货表分页数据")
    public ResponseEntity findAlldelivery_record(@ApiParam(name="currentPage",value = "当前页码",required = true )@PathVariable int currentPage,
                                                 @ApiParam(name = "pageSize",value = "页面大小",required = true)@PathVariable int pageSize){
            return productionWareHouseService.findAlldelivery_record(currentPage,pageSize);
    }
    @ApiOperation(value = "删除单条发货记录")
    @DeleteMapping("/delete/{shipment_No}")
    public ResponseEntity deleteOne(@ApiParam(name = "material_code", value="产品编码")@PathVariable String shipment_No){
        return productionWareHouseService.deleteOne(shipment_No);
    }
    @PutMapping("/productionwarehouse")
    @ApiOperation(value = "修改发货记录")
    public ResponseEntity updateOne(@ApiParam(name = "deliveryrecord",value = "修改发货记录")@RequestBody Deliveryrecord deliveryrecord){
        return productionWareHouseService.updateOne(deliveryrecord);
    }
    @GetMapping("/findShipment_No/{shipment_No}")
    @ApiOperation(value = "查找发货记录")
    public ResponseEntity findShipment_No(@ApiParam(name = "shipment_No",value = "订单编号")@PathVariable String shipment_No){
            return productionWareHouseService.findShipment_No(shipment_No);
    }
    @PostMapping("/addStore")
    @ApiOperation(value = "新增入库")
    public ResponseEntity addStore(@ApiParam(name ="warehousingrecord",value = "新增入库")@RequestBody Warehousingrecord warehousingrecord){
        return productionWareHouseService.addStore(warehousingrecord);
    }
    @PutMapping("/store")
    @ApiOperation(value = "修改入库记录")
    public ResponseEntity updateStore(@ApiParam(name = "warehousingrecord",value = "修改入库记录")@RequestBody Warehousingrecord warehousingrecord){
           return productionWareHouseService.updateStore(warehousingrecord);
    }
    @DeleteMapping("/deleteStore/{receipt_No}")
    @ApiOperation(value = "删除一条入库记录")
    public  ResponseEntity deleteStore(@ApiParam(name = "receipt_No",value = "删除一条入库记录")@PathVariable String receipt_No){
        return productionWareHouseService.deleteStore(receipt_No);
    }
    @GetMapping("/findReceipt_No/{receipt_No}")
    @ApiOperation(value = "查找发货记录")
    public ResponseEntity findReceipt_No(@ApiParam(name = "receipt_No",value = "查询入库记录")@PathVariable String receipt_No){
        return productionWareHouseService.findReceipt_No(receipt_No);
    }
    @PutMapping("/warehouse_num")
    @ApiOperation(value = "增加仓库库存数量")
    public ResponseEntity updatewarehouse_num(@ApiParam(name = "warehousingrecord",value = "修改仓库数量")@RequestBody Warehousingrecord warehousingrecord){
        return productionWareHouseService.updatewarehouse_num(warehousingrecord.getMaterial_code(),warehousingrecord.getReceipt_quantity());
    }
    @PutMapping("/update")
    @ApiOperation(value = "减少仓库库存数量")
    public ResponseEntity update(@ApiParam(name = "deliveryrecord",value = "修改仓库数量")@RequestBody Deliveryrecord deliveryrecord){
        return productionWareHouseService.update(deliveryrecord.getMaterial_code(),deliveryrecord.getShipped_Qty());
    }
    @GetMapping("/inventorydetails/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取库存详情表")
    public ResponseEntity findinventorydetails(@ApiParam(name="currentPage",value = "当前页码",required = true ) @PathVariable int currentPage,
                                               @ApiParam(name = "pageSize",value = "页面大小",required = true)@PathVariable int pageSize){
        return productionWareHouseService.findinventorydetails(currentPage,pageSize);
    }
    @GetMapping("/findOrder")
    @ApiOperation(value = "获取库存详情表")
    public ResponseEntity findOrder(){
        return productionWareHouseService.findOrder();
    }
    @GetMapping("/findPW/{material_code}")
    @ApiOperation(value = "获取产品仓库详情表")
    public ResponseEntity findPW(@ApiParam(name = "material_code",value = "通过编码查找库存和仓库名称")@PathVariable String material_code){
        return productionWareHouseService.findPW(material_code);
    }
    @PutMapping("/updateOrderStatus")
    @ApiOperation(value = "修改订单状态")
    public ResponseEntity updateOrderStatus(@ApiParam(name = "deliveryrecord",value = "修改订单状态")@RequestBody Deliveryrecord deliveryrecord){
        return productionWareHouseService.updateOrderStatus(deliveryrecord.getOrder_num());
    }
    @PostMapping("/import")
    @ApiOperation(value = "从excel导入入库信息")
    public ResponseEntity importExcel(@ApiParam(name = "file", value="excel文件")@RequestPart("file") MultipartFile file) {
        List<Warehousingrecord> list = null;
        try {
            list = EasyExcel.read(file.getInputStream())
                    .head(Warehousingrecord.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return productionWareHouseService.importExcel(list);
    }
    @GetMapping("/export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("发货清单.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        EasyExcel.write(response.getOutputStream(), Delivery_record.class).sheet("模板")
                .doWrite((Collection<?>) productionWareHouseService.findAlldelivery_record().getData());
    }
    @GetMapping("/exportInventorydetails")
    @ApiOperation(value = "导出excel")
    public void exportInventorydetails(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("库存清单.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        EasyExcel.write(response.getOutputStream(),  Inventorydetails.class).sheet("模板")
                .doWrite((Collection<?>) productionWareHouseService.findinventorydetails().getData());
    }
}
